/*
 * @Author: your name
 * @Date: 2020-12-13 09:00:49
 * @LastEditTime: 2020-12-13 09:29:38
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \c\list.c
 */
#include <head.h>
#include "./linkist.h"
bool linklistaddNode (LinkList *list,DATATYPE data)
{
    listnode * p = (listnode *)malloc(sizeof(listnode));
    if(!p){
        // printf("malloc error\n");
        return false;
    }
    if(!list){
        printf("empty list!\n");
        return  false;
    }
    // printf("node addr %#X\n",(unsigned int )p);
    p->data =data;
    p->next = list->node;
    list->node = p;
    list->nodenum++;
    return true;
}

bool linklistdelnode(LinkList *list,DATATYPE data)
{
    if(!list){
        printf("empty list!\n");
        return  false;
    }
     listnode * p = list->node;
     listnode * prev =NULL;
     for (int i = 0; i < list->nodenum ; i++)
     {
         if(p->data == data){
             break;
         }
        prev  = p;
        p=p->next;
     }
    if(!prev){
        list->node = p->next;
    }
    else{
        prev->next =p->next;
        list->node =prev;
    }
    free(p);
    list->nodenum--;
    return true;
}
void printlinklist (LinkList *list)
{
    if(!list){
        printf("empty list!\n");
        return ;
    }
    listnode * p = list->node;
     for (int i = 0; i < list->nodenum ; i++)
     {
        printf("node addr %#X,data:%d\n",(unsigned int )p,p->data);
        p=p->next;
    }

}
LinkList l1 =LIST_INIT_VALUE;
void list_test()
{
    linklistaddNode(&l1,1);
    linklistaddNode(&l1,2);
    linklistaddNode(&l1,3);
    printlinklist(&l1);
    printf("del node 2\n");
    linklistdelnode(&l1,3);
    printlinklist(&l1);
}
